home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 November: Tool Chest / Dev.CD Nov 98 TC.toast / Sample Code / Snippets / Toolbox / Icon play / Source / PlotIcons.c
Encoding:
C/C++ Source or Header  |  1996-09-17  |  10.2 KB  |  373 lines  |  [TEXT/CWIE]

  1. // snippet to demonstrate the use of the system 7 icon utilities
  2. // and the system 7 popup control cdef in a program using modal dialogs
  3. //
  4. // This shows: how to use the sys 7.0 popup
  5. // and how to plot Icons from Icon families.
  6. //
  7. // Nick Thompson, 6 May '94
  8. //
  9. //    5/11/94        nt        updated to use std file icons, in another dialog box
  10. //
  11. //    Copyright:    © 1994 by Apple Computer, Inc., all rights reserved.
  12.  
  13. // updates 8/96: Prefix.h added as prefix file for 68K and PPC MC projects;
  14. // old routines names changed; Str255 theItem, OSErr theErr and short userSelection
  15. // removed from DoStdFileIconsDialog; line 266 - NewModalFilterProc added;
  16. // fixed return value for OurFilter to eliminate compiler warnings
  17.  
  18. #include <Menus.h>
  19. #include <Fonts.h>
  20. #include <Dialogs.h>
  21. #include <icons.h>
  22.  
  23. // prototypes
  24. pascal Boolean OurFilter(DialogPtr dlg, EventRecord *event, short *itemHit) ;
  25. void DoStdFileIconsDialog(void);
  26. void UpdateIconsDialog(    DialogPtr thePopupDialog );
  27. void    FlashDialogItem(DialogPtr dlg, short itemToFlash);
  28.  
  29. // constants used in the dialog filter proc
  30. const char kEnter    = 0x03 ;
  31. const char kReturn    = 0x0D ;
  32. const char kEscape    = 0x1B ;
  33. const char kPeriod    = '.' ;
  34.  
  35.  
  36. // constants for our dialog IDs these are used in the filterproc to do the "right thing"
  37. // by calling the appropriate redrawing code
  38.  
  39. const long    kIconsDialog        = 1000 ;
  40. const long    kStdFileIconsDialog    = 1001 ;
  41.  
  42.  
  43. // constants for items in the dialog
  44. enum {
  45.     kThePopupMenu = 4 ,        // popup menu is the fourth item in the dialog
  46.     kUserTextArea = 5 ,        // this is a rect for us to write a string with the user selection
  47.     kLargeIconArea = 8 ,    // rect to draw the lg icon into
  48.     kSmallIconArea = 9 ,    // rect to draw the small icon
  49.     kMoreIconBtn = 10         // we want to see the std file icon dialog
  50. } ;
  51.  
  52. // table to tie up the menu selection with the 
  53. // actual icon IDs defined in Icons.h
  54. // These are documented in the resource manager chapter
  55. // of Inside Macintosh: More Toolbox Essentials, Page 1-130 onwards.
  56.  
  57. long    resIDTable[] = {
  58.         0,                                    /* Dummy for start of the array */
  59.         genericDocumentIconResource,        /* [1]  "Generic Document" */
  60.         genericApplicationIconResource,        /* [2]  "Generic Application" */
  61.         genericDeskAccessoryIconResource,    /* [3]  "Generic Desk Accessory" */
  62.         genericEditionFileIconResource,        /* [4]  "Generic Edition File" */
  63.         genericStationeryIconResource,        /* [5]  "Generic Stationary" */
  64.         genericPreferencesIconResource,        /* [6]  "Generic Preferences" */
  65.         genericQueryDocumentIconResource,    /* [7]  "Generic Query Document" */
  66.         genericExtensionIconResource,        /* [8]  "Generic Extension" */
  67.         genericFolderIconResource,            /* [9]  "Generic Folder" */
  68.         privateFolderIconResource,            /* [10]  "Private Folder" */
  69.         ownedFolderIconResource,            /* [11]  "Owned Folder" */
  70.         dropFolderIconResource,                /* [12]  "Drop Folder" */
  71.         sharedFolderIconResource,            /* [13]  "Shared Folder" */
  72.         mountedFolderIconResource,            /* [14]  "Mounted Folder" */
  73.         systemFolderIconResource,            /* [15]  "System Folder" */
  74.         appleMenuFolderIconResource,        /* [16]  "Apple Menu Folder" */
  75.         startupFolderIconResource,            /* [17]  "Startup Items Folder" */
  76.         controlPanelFolderIconResource,        /* [18]  "Control Panels" */
  77.         printMonitorFolderIconResource,        /* [19]  "Printmonitor Folder" */
  78.         preferencesFolderIconResource,        /* [20]  "Preferences Folder" */
  79.         extensionsFolderIconResource,        /* [21]  "Extensions" */
  80.         -3968,                                /* [22]  "Fonts" */
  81.         floppyIconResource,                    /* [23]  "Floppy Icon" */
  82.         trashIconResource,                    /* [24]  "Trash Icon" */
  83.         fullTrashIconResource                /* [25]  "Full Trash Icon" */
  84. } ;
  85.  
  86. long    stdFileResTablep[] = {
  87.         1,
  88.         2,
  89.         3,
  90.         4,
  91.         5
  92. } ;
  93.  
  94.  
  95.  
  96. // Structure for the private data for a popup control.
  97. // This structure is documented on page 5-77 
  98. // Inside Macintosh: Macintosh Toolbox Essentials
  99.  
  100. typedef struct popupPrivateData {
  101.     MenuHandle     mHandle;     // the popup menu handle 
  102.     short         mID;        // the popup menu ID 
  103.     // after these two public fields is the mPrivate private data, 
  104.     // which may be any old size and should not be messed with 
  105. }    popupPrivateData;
  106.  
  107.  
  108. void DoIconsDialog(void) ;
  109.  
  110.  
  111. void main(void) {
  112.  
  113.     // initialize the toolbox
  114.     InitGraf(&qd.thePort); InitFonts(); InitWindows(); InitMenus();
  115.     TEInit(); InitDialogs(nil); InitCursor(); FlushEvents(everyEvent,0);
  116.     
  117.     
  118.     DoIconsDialog() ;
  119. }
  120.  
  121.  
  122. void DoStdFileIconsDialog(void) 
  123. {
  124.     popupPrivateData    **myPopupPrivateDataPtr ;
  125.     
  126.     short                iKind;
  127.     Handle                iHandle;
  128.     Rect                iRect;
  129.     
  130.     MenuHandle            thePopupMenuHdl ;
  131.     
  132.     DialogPtr             thePopupDialog ;
  133.     short                itemHit ;
  134.     GrafPtr                savedPort ;
  135.     
  136.     short                index ;
  137.  
  138.     thePopupDialog = GetNewDialog ( 129, nil, (WindowPtr)-1 );
  139.     SetWRefCon((WindowPtr)thePopupDialog, kStdFileIconsDialog ) ;
  140.     GetPort( &savedPort ) ;
  141.     SetPort( thePopupDialog ) ;
  142.  
  143.  
  144.     // get a reference to the popup control, this is item 4 in the dialog
  145.     // we need to get the menuhandle associated with the control, it is in the private
  146.     // control data field, as documented in Inside Macintosh: Toolbox page 5-77
  147.     
  148.     // get the control handle for the popup            
  149.     GetDialogItem ( thePopupDialog, kThePopupMenu, &iKind, &iHandle, &iRect) ;
  150.     
  151.     // extract from the control the menuhandle
  152.     myPopupPrivateDataPtr = (popupPrivateData **)(**(ControlHandle)iHandle).contrlData ; 
  153.     thePopupMenuHdl = (**myPopupPrivateDataPtr).mHandle ;
  154.     
  155.     // set up the Icons - we have 5 items here
  156.     for( index = 0;    index < 5; index++ ) 
  157.         SetItemIcon( thePopupMenuHdl, index + 1, stdFileResTablep[index]) ;
  158.     do {
  159.     
  160.         ModalDialog ( (ModalFilterUPP) OurFilter, &itemHit );
  161.         
  162.         switch ( itemHit ) {
  163.         
  164.             default :
  165.                 break ;        
  166.         }
  167.         
  168.     } while( itemHit != ok ) ;
  169.     
  170.     DisposeDialog ( thePopupDialog );
  171.     SetPort( savedPort ) ;
  172.  
  173. }
  174.  
  175.  
  176.  
  177. // this function ensures that the correct Icons are drawn in the dialog
  178. // corresponding with the current user selection.
  179.  
  180. void UpdateIconsDialog(    DialogPtr thePopupDialog )     // dialog pointer for the popup dialog
  181. {
  182.     popupPrivateData    **myPopupPrivateDataPtr ;
  183.     
  184.     short                iKind;
  185.     Handle                iHandle;
  186.     Rect                iRect;
  187.     
  188.     MenuHandle            thePopupMenuHdl ;
  189.     Str255                theItem ;
  190.  
  191.     OSErr                theErr ;
  192.     
  193.     
  194.     short                userSelection ;            // the current menu selection
  195.     
  196.     // the user choose the popup.  The item number selected will be the control value
  197.     // we need to get the menuhandle associated with the control, it is in the private
  198.     // control data field, as documented in Inside Macintosh: Toolbox page 5-77
  199.     
  200.     // get the control handle for the popup            
  201.     GetDialogItem ( thePopupDialog, kThePopupMenu, &iKind, &iHandle, &iRect) ;
  202.     
  203.     // extract from the control the menuhandle
  204.     myPopupPrivateDataPtr = (popupPrivateData **)(**(ControlHandle)iHandle).contrlData ; 
  205.     thePopupMenuHdl = (**myPopupPrivateDataPtr).mHandle ;
  206.  
  207.     // get the string associated with the users selection
  208.     userSelection = GetControlValue((ControlHandle)iHandle) ;
  209.     GetMenuItemText ( thePopupMenuHdl, userSelection, theItem );
  210.     
  211.     // get the rect we are drawing in
  212.     GetDialogItem ( thePopupDialog, kUserTextArea, &iKind, &iHandle, &iRect) ;
  213.     
  214.     // erase the area to draw in
  215.     EraseRect( &iRect ) ;
  216.                 
  217.     // and draw the current user selection
  218.     MoveTo(iRect.left + 1, iRect.bottom - 6) ;
  219.     DrawString( theItem ) ;
  220.     
  221.     // now plot the Icons.  The id of the item selected in
  222.     // the menu will be in userSelection.
  223.     
  224.     // first draw the large icon
  225.     
  226.     // get the rect we are drawing in
  227.     GetDialogItem ( thePopupDialog, kLargeIconArea, &iKind, &iHandle, &iRect) ;
  228.  
  229.     // erase the area to draw in
  230.     EraseRect( &iRect ) ;
  231.  
  232.     theErr = PlotIconID( &iRect, atVerticalCenter, ttNone, resIDTable[ userSelection ] ) ;
  233.     
  234.     // now draw the small icon
  235.     
  236.     // get the rect we are drawing in
  237.     GetDialogItem ( thePopupDialog, kSmallIconArea, &iKind, &iHandle, &iRect) ;
  238.  
  239.     // erase the area to draw in
  240.     EraseRect( &iRect ) ;
  241.  
  242.     theErr = PlotIconID( &iRect, atVerticalCenter, ttNone, resIDTable[ userSelection ] ) ;
  243.  
  244.  
  245.  
  246. }
  247.  
  248.  
  249. void DoIconsDialog(void) 
  250. {
  251.     DialogPtr             thePopupDialog ;
  252.     short                itemHit ;
  253.     GrafPtr                savedPort ;
  254.     ModalFilterUPP        modalFilterUPP;
  255.  
  256.     modalFilterUPP = NewModalFilterProc(OurFilter);
  257.     if (modalFilterUPP)
  258.     {
  259.         thePopupDialog = GetNewDialog ( 128, nil, (WindowPtr)-1 );
  260.         if (thePopupDialog)
  261.         {
  262.             SetWRefCon((WindowPtr)thePopupDialog, kIconsDialog ) ;
  263.             GetPort( &savedPort ) ;
  264.             SetPort( thePopupDialog ) ;
  265.             UpdateIconsDialog( thePopupDialog ) ;
  266.  
  267.  
  268.             // get a reference to the popup control, this is item 4 in the dialog
  269.             
  270.             do {
  271.             
  272.                 ModalDialog ( modalFilterUPP, &itemHit );
  273.                 
  274.                 switch ( itemHit ) {
  275.                 
  276.                     case kThePopupMenu :
  277.                     
  278.                         UpdateIconsDialog( thePopupDialog ) ;
  279.                         // reset itemHit to something else or we'll continually redraw
  280.                         itemHit = 0 ;
  281.                         break ;
  282.                         
  283.                     case kMoreIconBtn :
  284.                         
  285.                         SetPort( savedPort ) ;
  286.                         DoStdFileIconsDialog() ;
  287.                         SetPort( thePopupDialog ) ;
  288.                         break ;
  289.                 
  290.                 }
  291.                 
  292.             } while( itemHit != ok ) ;
  293.             
  294.             DisposeDialog ( thePopupDialog );
  295.             SetPort( savedPort ) ;
  296.         }
  297.         DisposeRoutineDescriptor (modalFilterUPP);
  298.     }
  299. }
  300.  
  301.  
  302. void    FlashDialogItem(DialogPtr dlg, short itemToFlash)
  303. {
  304.     short    iKind;
  305.     Handle    iHandle;
  306.     Rect    iRect;
  307.     long    ignored;
  308.  
  309.     GetDialogItem(dlg, itemToFlash, &iKind, &iHandle, &iRect);
  310.     HiliteControl((ControlHandle) iHandle, 1);
  311.     Delay(8, &ignored);
  312.     HiliteControl((ControlHandle) iHandle, 0);
  313. }
  314.  
  315.  
  316.  
  317.  
  318. pascal Boolean OurFilter(DialogPtr dlg, EventRecord *event, short *itemHit)
  319. {
  320.     Boolean    result = false;
  321.     short    iKind;
  322.     Handle    iHandle;
  323.     Rect    iRect;
  324.     char    key;
  325.     short    radius;
  326.  
  327.     switch (event->what) {
  328.  
  329.         case keyDown:
  330.         case autoKey:
  331.             key = event->message & charCodeMask;
  332.             if (event->modifiers & cmdKey) {        /* Command key down */
  333.                 if (key == kPeriod) {
  334.                     *itemHit = ok;
  335.                     FlashDialogItem(dlg, *itemHit);
  336.                 }
  337.                 result = true;            /* This says we handle ALL command keys */
  338.             } else {
  339.                 if ((key == kReturn) || (key == kEnter)) {
  340.                     *itemHit = ok;
  341.                     FlashDialogItem(dlg, *itemHit);
  342.                     result = true;
  343.                 }
  344.                 else if (key == kEscape) {
  345.                     *itemHit = ok;
  346.                     FlashDialogItem(dlg, *itemHit);
  347.                     result = true;
  348.                 }
  349.             }
  350.             break;
  351.  
  352.         case updateEvt:
  353.             SetPort(dlg);
  354.                 
  355.             GetDialogItem(dlg, ok, &iKind, &iHandle, &iRect);            
  356.             InsetRect(&iRect, -4, -4);
  357.             radius = (iRect.bottom - iRect.top) / 2;
  358.             if (radius < 16)
  359.                 radius = 16;
  360.                 
  361.             PenSize(3,3);
  362.             FrameRoundRect(&iRect, radius, radius);
  363.             PenNormal();
  364.             
  365.             if(    GetWRefCon((WindowPtr)dlg ) == kIconsDialog ) 
  366.                 UpdateIconsDialog( dlg ) ;
  367.             break;
  368.     }
  369.  
  370.     return result;
  371. }
  372.  
  373.